package utilidade;


import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import repositorio.ExcecaoConexaoBancoDados;


public class JPAUtil {
	
	private static EntityManagerFactory emFactory = null;
	
	public static EntityManagerFactory getEntityManagerFactory() throws ExcecaoConexaoBancoDados {
			
		try{
		
			if(emFactory==null || (!emFactory.isOpen())) {
				
				emFactory = Persistence.createEntityManagerFactory("conexao");				
			}			
			
			return emFactory;
			
		} catch (Exception e) {
			
			System.out.println("Falha na conexao: "+e.getMessage());
			
			throw new ExcecaoConexaoBancoDados();
		}
	}
	
	public static void closeEntityManagerFactory() {
		emFactory.close();
	}
	
}
